package com.rk.pethome.product.controller;

import com.rk.pethome.basic.util.AjaxResult;
import com.rk.pethome.basic.util.PageList;
import com.rk.pethome.product.domain.Product;
import com.rk.pethome.product.query.ProductQuery;
import com.rk.pethome.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RequestMapping("/product")
@RestController
public class ProductController {

    @Autowired
    IProductService iProductService;


    /**
     * 保存/修改服务
     * @param product
     * @return
     */
    @PostMapping
    public AjaxResult save(@RequestBody Product product) {
        System.out.println(product);
        try {
            iProductService.addOrUpdate(product);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /**
     * 修改服务
     * @param product
     * @return
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Product product) {
        try {
            iProductService.addOrUpdate(product);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }


    /**
     *批量删除数据
     * 服务集合
     * @return
     */
    @PatchMapping("{id}")
    public AjaxResult batchRemove(@RequestBody List<Product> products) {
        try {
            iProductService.batchRemove(products);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /**
     *根据id删除对象
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable("id")Long id) {
        try {
            iProductService.delete(id );
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /**
     *根据id查询对象
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Product loadById(@PathVariable("id")Long id)
    {
       return iProductService.loadById(id);
    }



    /**
     *查询所有对象
     * @param
     * @return
     */
    @PatchMapping
    public PageList<Product> loadPageQuery(@RequestBody ProductQuery query) {
        System.out.println(query);
        return iProductService.loadPagebyQuery(query);
    }

    /**
     *查询所有的服务列表
     * @return
     */
    @PatchMapping("/loadAll")
    public List<Product>  loadAll(){
        return iProductService.loadAll();
    }

}
